package totask.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import totask.dao.UserMapper;
import totask.dao.UserRoleMapper;
import totask.pojo.TotaskRole;
import totask.pojo.TotaskUser;
import totask.service.UserService;
import totask.utils.UuidUtil;
import totask.vo.UserInfoVO;

import java.util.List;
import java.util.Objects;

@Service
@Transactional//该类所有方法支持事务
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    private StringEncryptor stringEncryptor;


    @Override
    public List<TotaskUser> getUserAll() {
        List<TotaskUser> totaskUsers = userMapper.selectList(null);
        //密码解密
        for (TotaskUser totaskUser : totaskUsers) {
            String userPassword = stringEncryptor.decrypt(totaskUser.getPassword());
            totaskUser.setPassword(userPassword);
        }
        return totaskUsers;
    }

    @Override
    public boolean addUser(TotaskUser totaskUser) {
        totaskUser.setUserId(UuidUtil.getShortUuid());
        //加密密码
        totaskUser.setPassword(stringEncryptor.encrypt(totaskUser.getPassword()));
        return userMapper.insert(totaskUser) == 1 ? true : false;
    }

    @Override
    public UserInfoVO userLogin(String userName, String passWord) {
        //1.根据用户名查询密码
        QueryWrapper<TotaskUser> query = new QueryWrapper();
        query.eq("username",userName);
        TotaskUser totaskUser = userMapper.selectOne(query);
        //判断是TotaskUser是否为空
        if(Objects.isNull(totaskUser)){
            return null;
        }else {
            //2.获取密码3.解密
            String depassword = stringEncryptor.decrypt(totaskUser.getPassword());
            //判断是否一致
            if (depassword.equals(passWord)){
                //查询用户角色表
                QueryWrapper<TotaskRole> TotaskRoleQuery = new QueryWrapper();
                TotaskRoleQuery.eq("role_id",totaskUser.getTotaskRole());
                TotaskRole totaskRole = userRoleMapper.selectOne(TotaskRoleQuery);
                //复制对象
                UserInfoVO userInfo = new UserInfoVO();
                BeanUtil.copyProperties(totaskUser,userInfo);
                BeanUtil.copyProperties(totaskRole,userInfo);
                return userInfo;
            }else {
                //密码不一致
                return null;
            }
        }

    }

    @Override
    public TotaskUser getByUser(String userId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        TotaskUser totaskUser = userMapper.selectOne(queryWrapper);
        return totaskUser;
    }

    @Override
    public List<TotaskUser> queryRoleUser(Integer roleId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("totask_role",roleId);
        List<TotaskUser>  TotaskUserlist = userMapper.selectList(queryWrapper);
        return TotaskUserlist;
    }
}
